package com.sk.bilibili.utils;


import android.content.Context;
import android.content.Intent;
import android.net.Uri;

import com.sk.bilibili.view.activity.MemberActivity;
import com.sk.bilibili.view.activity.ThemActivity;
import com.sk.bilibili.view.activity.VideoSetActivity;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class IntentUtils {

    public static Intent createShareIntent(String content){
        Intent intent = new Intent(Intent.ACTION_SEND);
        intent.setType("text/plain");
        intent.putExtra(Intent.EXTRA_TEXT,content);
        return intent;
    }

    //      http://82.157.168.147/space/1
    private static final Pattern HANDLE_PATTERN=Pattern.compile("http.+/([a-z]+)/(\\d+)(/(\\d+))?");

    public static void startUrl(Context context, String url) {
        Matcher matcher = HANDLE_PATTERN.matcher(url);
        if(matcher.find()){
            String name = matcher.group(1);
            Integer id=parseInt(matcher.group(2));
            int selection=parseInt(matcher.group(4));
            switch (name){
                case "video":VideoSetActivity.start(context,id,selection);break;
                case "play":ThemActivity.start(context,id,selection);break;
                case "space":MemberActivity.start(context,id);break;
                default:openUrl(context,url);break;
            }
        }else {
            openUrl(context,url);
        }
    }

    private static int parseInt(String num) {
        if(num==null)return 0;
        return Integer.parseInt(num);
    }

    private static void openUrl(Context context, String url) {
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_VIEW);  //设置要操作的Action
        intent.setData(Uri.parse(url)); //要设置的数据
        context.startActivity(intent);
    }

}













